using UnityEngine;

namespace Script.Audio
{
    public class BGAudio : AudioBase
    {
        private AudioSource audioSource;


        private void Awake()
        {
            Bind(AudioEvent.PLAY_BG_AUDIO,
                AudioEvent.SET_BG_VOLUME,
                AudioEvent.STOP_BG_AUDIO);

            audioSource = transform.GetComponent<AudioSource>();
        }

        public override void Execute(int eventCode, object message)
        {
            switch (eventCode)
            {
                case AudioEvent.PLAY_BG_AUDIO:
                    playAudio();
                    break;
                case AudioEvent.SET_BG_VOLUME:
                    setVolume((float) message);
                    break;
                case AudioEvent.STOP_BG_AUDIO:
                    stopAudio();
                    break;
            }
        }


        private void setVolume(float _value)
        {
            if (audioSource != null)
            {
                audioSource.volume = _value;
            }
        }

        private void stopAudio()
        {
            if (audioSource != null)
            {
                audioSource.Stop();
            }
        }


        private void playAudio()
        {
            if (audioSource != null)
            {
                audioSource.Play();
            }
        }
    }
}